import java.util.Scanner;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘远望
 * Date: 2025-04-12
 * Time: 18:14
 */
class MyMinStatic{
    //最小栈
    public Stack<Integer> stack = new Stack<>();
    public Stack<Integer> minStack = new Stack<>();

    public void push(int val){
        stack.push(val);
        if(minStack.empty()){
            minStack.push(val);
        }else {
            int peekVal = minStack.peek();
            if(val <= peekVal){
                minStack.push(val);
            }
        }
    }
    public void pop(){
        if(stack.empty()){
            return;
        }
        int popVal = stack.pop();
        if(popVal == minStack.peek()){
            minStack.pop();
        }
    }
    public int top(){
        if(stack.empty()){
            return -1;
        }
        return Stack.peek();
    }
    public int minStack(){
        if(minStack.empty()){
            return -1;
        }
        return minStack.peek();
    }
}
//栈的练习题
public class Test {

    //判断是否为出栈序列
    public static boolean IsPopOrder(int[] pushv, int[] popv){
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i < pushv.length; i++){
            stack.push(pushv[i]);
            while(!stack.empty() && stack.peek() == popv[j] && j < popv.length) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
    public static void main(String[] args) {
        int[] push = {1,2,3,4,5};
        int[] pop = {4,5,3,2,1};
        boolean ret = IsPopOrder(push , pop);
        System.out.println(ret);
    }
    //
    public static int evalRPN(String[] strs){
        Stack<Integer> stack = new Stack<>();
        for(String s : strs){
            if(!isOperator(s)){
                int x = Integer.parseInt(s);
                stack.push(x);
            }else{
                int a = stack.pop();
                int b = stack.pop();
                switch (s){
                    case "+":
                        stack.push(b + a);
                        break;
                    case "-":
                        stack.push(b - a);
                        break;
                    case "*":
                        stack.push(b * a);
                        break;
                    case "/":
                        stack.push(b / a);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public static boolean isOperator(String s){
        if(s == "+" || s == "-" || s == "*" || s == "/"){
            return true;
        }else {
            return false;
        }
    }
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();
        String[] arr = new String[len];
        for(int i = 0 ; i < len ; i++) {
            String str = scanner.nextLine();
            arr[i] = str;
        }
        //String[] arr = { "1", "2", "3","*", "+", "4", "5", "*", "6", "+", "7", "*", "+"};
        int ret = evalRPN(arr);

        System.out.println(ret);
    }

    //判断括号
    public static boolean isValid(String str){
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < str.length();i++){
            char ch = str.charAt(i);
            if(ch == '{' || ch == '[' || ch == '('){
                stack.push(ch);
            }else {
                if (stack.empty()) {
                    return false;
                }
                //此时判断是否匹配
                char ch2 = stack.peek();
                if (ch == ')' && ch2 == '(' ||
                        ch == '}' && ch2 == '{' ||
                        ch == ']' && ch2 == '[') {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
        return true;
    }
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        boolean ret = isValid(str);
        System.out.println(ret);
    }
}
